﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace AssData
{
    public class DataSelect
    {
        AppData.EventDataSetTableAdapters.EventTableAdapter MyEvent = new AppData.EventDataSetTableAdapters.EventTableAdapter();
        AppData.EventDataSetTableAdapters.SettingTableAdapter MySet = new AppData.EventDataSetTableAdapters.SettingTableAdapter();
        #region 公共方法
        /// <summary>
        /// 获取事件单一标识
        /// </summary>
        /// <param name="title"></param>
        /// <param name="startime"></param>
        /// <param name="endtime"></param>
        /// <param name="location"></param>
        /// <returns></returns>
        public string GetUniqueSN(string title, DateTime startime, DateTime endtime, string location)
        {
            Byte[] sn= System.Text.Encoding.Default.GetBytes(title + startime.ToString() + endtime.ToString() + location);
            string ansi = "";
            foreach (byte a in sn)
                ansi += a;
            return ansi;
        }
        #endregion
        #region 事件数据操作
        /// <summary>
        /// 获取所有日程
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllEventData()
        {
            return MyEvent.GetAllData();
        }
        /// <summary>
        /// 根据时间获取事件
        /// </summary>
        /// <param name="datetime"></param>
        /// <returns></returns>
        public DataTable GetEventByDate(string starttime,string endtime)
        {
            return MyEvent.GetEventByDate(DateTime.Parse(starttime),DateTime.Parse(endtime),DateTime.Parse(starttime),DateTime.Parse(endtime));
        }
        public EventModel GetEventByID(int id, string unique)
        {
            DataTable dt = MyEvent.GetDataByID(id, unique);
            if (dt.Rows.Count > 0)
            {
                EventModel model = new EventModel();
                model.ID = int.Parse(dt.Rows[0][0].ToString());
                //model.UniqueSN = dt.Rows[0][1].ToString();
                model.EventTitle = dt.Rows[0][2].ToString();
                model.EventMark = dt.Rows[0][3].ToString();
                model.EventRelate = dt.Rows[0][4].ToString();
                model.EventLocation = dt.Rows[0][5].ToString();
                if (dt.Rows[0][6].ToString() != "")
                    model.Startime = DateTime.Parse(dt.Rows[0][6].ToString());
                if (dt.Rows[0][7].ToString() != "")
                    model.Endtime = DateTime.Parse(dt.Rows[0][7].ToString());
                model.State = dt.Rows[0][8].ToString();
                if (dt.Rows[0][9].ToString() != "")
                    model.Addtime = DateTime.Parse(dt.Rows[0][9].ToString());
                if (dt.Rows[0][10].ToString() != "")
                    model.AheadMin = int.Parse(dt.Rows[0][10].ToString());
                model.Duplicate = dt.Rows[0][11].ToString();
                model.AllDayEvent = bool.Parse(dt.Rows[0][12].ToString());
                return model;
            }
            else
                throw new Exception("数据库数据错误！");
        }
        /// <summary>
        /// 新建事件
        /// </summary>
        /// <param name="ModelEvent"></param>
        /// <returns></returns>
        public bool InsertEvent(EventModel ModelEvent)
        {
            if (MyEvent.InsertEvent(ModelEvent.UniqueSN, ModelEvent.EventTitle, ModelEvent.EventMark, ModelEvent.EventRelate, 
                ModelEvent.EventLocation, ModelEvent.Startime, ModelEvent.Endtime, ModelEvent.State, ModelEvent.Addtime, 
                ModelEvent.AheadMin, ModelEvent.Duplicate, ModelEvent.AllDayEvent) > 0)
                return true;
            return false;
        }
        /// <summary>
        /// 删除事件
        /// </summary>
        /// <param name="id"></param>
        /// <param name="sn"></param>
        /// <returns></returns>
        public bool DelEvent(int id, string sn)
        {
            if (MyEvent.DeleteEvent(id, sn) > 0)
                return true;
            return false;
        }
        /// <summary>
        /// 更新事件
        /// </summary>
        /// <param name="ModelEvent"></param>
        /// <returns></returns>
        public bool UpdateEvent(EventModel ModelEvent)
        {
            if (MyEvent.UpdateEvent(ModelEvent.EventTitle, ModelEvent.EventMark, ModelEvent.EventRelate,
                ModelEvent.EventLocation, ModelEvent.Startime, ModelEvent.Endtime, ModelEvent.State, ModelEvent.Addtime,
                ModelEvent.AheadMin, ModelEvent.Duplicate, ModelEvent.AllDayEvent, ModelEvent.ID, ModelEvent.UniqueSN) > 0)
                return true;
            return false;
        }
        #endregion
    }
}
